﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;
using System.Security.Cryptography.X509Certificates;
using System.IO;

namespace DataAccess
{
    public partial class DataAccess
    {
        public DataAccessResult CreateDeploy(String SubscriptionID, String CertificatePath, String ServiceName, String DeploymentSlot, String DeployName, String PkgUrl, String ConfigFile, String Label)
        {

            DataAccessResult result = new DataAccessResult();
            string requestBodyFormat = @"<?xml version=""1.0"" encoding=""utf-8""?>
<CreateDeployment xmlns=""http://schemas.microsoft.com/windowsazure"">
  <Name>{0}</Name>
  <PackageUrl>{1}</PackageUrl>
  <Label>{2}</Label>
  <Configuration>{3}</Configuration>
</CreateDeployment>
";

            String requestUrl = "https://management.core.windows.net/" + SubscriptionID + "/services/hostedservices/" + ServiceName + "/deploymentslots/" + DeploymentSlot;

            WebResponse resp = null;
            try
            {

                FileStream fs = new FileStream(ConfigFile, FileMode.Open, FileAccess.Read);

                byte[] filebytes = new byte[fs.Length];
                fs.Read(filebytes, 0, Convert.ToInt32(fs.Length));
                string encodedData = Convert.ToBase64String(filebytes);
                String configData = encodedData;

                byte[] txtByte = System.Text.Encoding.Default.GetBytes(Label);
                Label = System.Convert.ToBase64String(txtByte);

                String requestBody = string.Format(requestBodyFormat, DeployName, PkgUrl, Label, configData);
                string ReturnBody = string.Empty;

                NameValueCollection RequestHeaders = new NameValueCollection();
                RequestHeaders.Add("x-ms-version", x_ms_version);


                X509Certificate cert = X509Certificate.CreateFromCertFile(CertificatePath);


                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
                request.Method = "POST";
                request.ClientCertificates.Add(cert);
                request.ContentType = "application/xml";
                //request.ContentLength = Encoding.UTF8.GetBytes(requestBody).Length;
                if (RequestHeaders != null)
                {
                    request.Headers.Add(RequestHeaders);
                }

                using (StreamWriter sw = new StreamWriter(request.GetRequestStream()))
                {
                    sw.Write(requestBody);
                    sw.Close();
                }
                resp = request.GetResponse();
                if (resp != null)
                {
                    HttpWebResponse httpResp = (HttpWebResponse)resp;
                    result.IsOperationSuccessful = httpResp.StatusCode.Equals(HttpStatusCode.Accepted);
                }
            }
            catch (WebException webEx)
            {
                result.IsOperationSuccessful = false;
                result.Error = webEx;
            }
            catch (IOException ex1)
            {
                result.IsOperationSuccessful = false;
                result.Error = ex1;
            }

            catch (Exception excep)
            {
                result.IsOperationSuccessful = false;
                result.Error = excep;
            }

            finally
            {
                //req = null;
                if (resp != null) { resp.Close(); }
                resp = null;
            }

            return result;
        }
    }
}
